
import flash.events.Event;
import flash.events.NetStatusEvent;
import flash.net.NetConnection;
import flash.net.NetStream;


//PHASE 1: SELECT TAG
	include "as3.as";
	
	function select_tag(select:String):void{
		var str:String = '';
		for(var i=0; i<iconTags.length; i++)
		{
			if(select==iconTags[i])
			{	
				if (iconTagsSelect[i]==0)
					iconTagsSelect[i]=1;
				else
					iconTagsSelect[i]=0;
			}
			if (iconTagsSelect[i]==1)
				str = str + iconTags[i] + ',';
		}
		if (str=='')
			number.text = '0';
		else
		{
			loader = new URLLoader();
			urlRequest = new URLRequest("http://localhost:8000/tag/");
			loader.addEventListener(Event.COMPLETE, completeHandler);
			var params:URLVariables = new URLVariables();
			params.tag = str;
					
			urlRequest.data = params;
			urlRequest.method = URLRequestMethod.POST;
			loader.load(urlRequest);
		}
		flag = 0;
		if(timerCnt==0)
		{
			var readyTimer:Timer = new Timer(100);
       		readyTimer.addEventListener(TimerEvent.TIMER, timerHandler);
        	readyTimer.start();
			timerCnt++;
		}
	}
	var timerCnt:int=0;
	function timerHandler(event:TimerEvent){
		var nstime:int = Math.floor(ns.time);
		if(flag==0)
		{	
			seekSlider.value = nstime;
			nowtime.text = nstime.toString();//ns.time.toString();
			mySliderLoading.width = (nsClip.bytesLoaded/nsClip.bytesTotal)*mySliderBar.width;
			mySlider.width = nstime/nsClipDuration*mySliderBar.width;
			circle.x = 18+nstime/nsClipDuration*(mySliderBar.width);
		}
		
		else if(flag==1)
		{
			var sofar:int = myStoryPlayingTime+nstime;
	//		seekSlider.value = sofar;
			for(var i=0; i<nsarray.length; i++)
			{
				Canvas2SliderLoading[i].width = (nsarray[i].bytesLoaded/nsarray[i].bytesTotal)*myStoryDurations[i]/myStoryDuration[0]*mySlider.width;
			}
			
			nowtime.text = sofar.toString();
			var rat:Number = nstime/myStoryDurations[myStoryCurrentClip];
			
			Canvas2Dot.x = rat*100+(myStoryCurrentClip)*(120+1);//(myStoryCurrentClip)*120+(Canvas2.x+120/2-10);	
		}
	}
	function completeHandler(event:Event):void {
		var response:URLLoader = URLLoader(event.target);
		var responseData:String = response.data;
    	_path = responseData.split(',');
				
		var num:int = _path.length;
		if (_path[num-1]=='')
	    	_path.pop();
		number.text = _path.length.toString();
	    number2.text = _path.length.toString();
	}
	function start_main():void{
		loadMovie(_path);
    	searchTag = false;
    	transition();
	}
	
	
	function loadMovie(_path:Array):void {

		Canvas1.removeAllChildren();
        ncClip = new NetConnection();
		ncClip.connect(null);
		nsClip = new NetStream(ncClip);
		nsClip.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler);
		
		volumeTransform = new SoundTransform();
		volumeSliderCreation();
		var nsClient:Object = {};
        nsClient.onMetaData = ns_onMetaData;
        nsClip.client = nsClient;
		
		nsClip.addEventListener(NetStatusEvent.NET_STATUS, clipNetStatusHandler);
		
		var pp = _path[0].split(".");
		nsClip.play("/mysite/data/"+pp[0]+".flv");
		play.visible = false;
		pause.visible = true;
	
		ns = nsClip;
		vid.attachNetStream(ns);
		vid.height = 9*42;
		vid.width = 16*42;
		
		init(_path);
			
	}
	function clipNetStatusHandler(event:NetStatusEvent):void{
		if(event.info.code=='NetStream.Play.Stop')
		{	
			play.visible = true;
			pause.visible = false;
//			nsClip.seek(0);
//			nsClip.pause();
		}
	}
	var nsClipDuration:int = 0;
	function ns_onMetaData(item:Object):void {
        seekSliderCreation(int(item.duration));
        nsClipDuration = int(item.duration);
        totaltime.text = int(item.duration).toString();		
    }

	var designedWid:Array = new Array();

	function init(_path:Array):void 
	{
        var main:Canvas = Canvas(this.Canvas1);
        		
        var check:int = Canvas1ImWid*_path.length+(_path.length-1);
        var wid:int = Math.floor((main.width+1-_path.length)/_path.length);
        		
        var empty:int = main.width - _path.length*(wid+1);
        designedWid = [];
        
       // new MOVE object Canvas2	
        Canvas2Move = new Move();
        		
        if (check<=main.width)
        {
        	for(var j=0; j<_path.length; j++)
        	{	designedWid[j]=Canvas1ImWid;	}
        }
        else
        {
	        for(var j=0; j<_path.length; j++)
    	  	{	    					
        		if (j<empty)
        			designedWid[j]=wid+1;	
        		else
        			designedWid[j]=wid;	
        	}        		
        }
    			
    	move1=new Array();
    	move2=new Array();
    	reSize = new Array();
    	originalX = new Array();
    	Canvas1Im = new Array();
//    	globalCanvas1ImX = new Array();
    	globalCanvas1MaskX = new Array();
    			
        for (var i=0; i<_path.length; i++)
        {
        	var ii:Image = new Image();
        	var aa = _path[i].split('.');
					
        	ii.source="/mysite/data/"+aa[0]+".jpg"; //'image/'+_path[i];
        	ii.maintainAspectRatio = false;
        	ii.height = Canvas1ImWid*3/4;
        	ii.width = Canvas1ImWid;
        	ii.id=i.toString();
        	ii.addEventListener(MouseEvent.CLICK, thumb_click);

	  		ii.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
        	ii.addEventListener(DragEvent.DRAG_COMPLETE, dragCompleteHandler);
        	ii.addEventListener(MouseEvent.MOUSE_MOVE, Animation);
			
        	var mask_x:int=0;
        	for(var k=0; k<i; k++)
        	{	 
        		mask_x = mask_x + designedWid[k]; ;
        	}
        	ii.x = mask_x+i+Math.floor(designedWid[i]/2)-ii.width/2;
        	ii.y = 0;
        					
        	originalX[i]=ii.x;
        			
        	var CanvasMask:Canvas = new Canvas();
        	CanvasMask.x = mask_x+i;
        	CanvasMask.y = ii.y;
        	CanvasMask.width = designedWid[i];
        	CanvasMask.height = ii.height;
        	CanvasMask.setStyle("backgroundColor", 0xFF00FF);
			main.addChild(ii);
			main.addChild(CanvasMask);
  			ii.mask = CanvasMask;
  			ii.id = "img" + i.toString();
  		
  			Canvas1Im.push(ii); 	
  					
  	//		globalCanvas1ImX[i+1] = ii.x;
  			globalCanvas1MaskX[i] = CanvasMask.x;
  			
  								
  		//Effects
  			var _move1:Move = new Move();
  			var _move2:Move = new Move();
  			var _reSize:Resize = new Resize();
  			_move1.target = ii;
  			_move2.target = CanvasMask;
  			_reSize.target = CanvasMask;
					
  			move1.push(_move1);
  			move2.push(_move2);
  			reSize.push(_reSize);
  	
        }	
		
	}
    
	function asyncErrorHandler(event:AsyncErrorEvent):void{
			    // ignore error
	}
	
	//PHASE 2: SELECT TAGGED VIDEO TO WATCH 			
			function thumb_click(event:Event):void{
				flag=0;
        		var temp = event.currentTarget.source.split('.');
        		ns.pause();
        		
        		nsClip.play(temp[0]+".flv");
        		ns=nsClip;
        		vid.attachNetStream(ns);
        		
        		play.visible = false;
				pause.visible = true;
        		loader2 = new URLLoader();
				urlRequest2 = new URLRequest("http://localhost:8000/video_info/");
				loader2.addEventListener(Event.COMPLETE, completeHandler2);
				var params:URLVariables = new URLVariables();
				temp = temp[0].split('/');
				params.video = temp[3];
				urlRequest2.data = params;
				urlRequest2.method = URLRequestMethod.POST;
				loader2.load(urlRequest2);
				
        	}
        	function completeHandler2(event:Event):void {
        		var response:URLLoader = URLLoader(event.target);
				var a:Array = response.data.split('||');
				var aa:Array = a[2].split(' ');
				var i:int=0;
				title.text = '';
				tag.text = '';
				des.text = '';
				
				title.text = a[0];
				while(i<aa.length)
				{	
					tag.text = tag.text + '\n' + aa[i];
					i++;
				}
				des.text = a[1];
        	}
        	function Canvas2Thumb_click(event:Event):void{
        		flag=0;
        		var temp = event.currentTarget.source.split('.');
        		ns.pause();
        		
        		nsClip.play(temp[0]+".flv");
        		ns=nsClip;
        		vid.attachNetStream(ns);
        		
        		play.visible = false;
				pause.visible = true;
				var a:MouseEvent = MouseEvent(event);
				var num:int = 0;
				for(var i=0; i<Canvas2ImSort.length; i++)
				{
					if (Canvas2ImSort[i]==event.currentTarget)
						num = i;
				}
				Canvas2Dot.x = num*(120+1)+50;
        /*
        		loader2 = new URLLoader();
				urlRequest2 = new URLRequest("http://localhost:8000/video_info/");
				loader2.addEventListener(Event.COMPLETE, completeHandler2);
				var params:URLVariables = new URLVariables();
				temp = temp[0].split('/');
				params.video = temp[3];
				urlRequest2.data = params;
				urlRequest2.method = URLRequestMethod.POST;
				loader2.load(urlRequest2);
        */
        	}
        	
        	
     // ActionScript file
